{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实验4  熟悉和使用NumPy模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "\n",
    "arr_a = np.arange(8).reshape(2, 4)\n",
    "print(arr_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.87436652 0.63975893 0.20699468 0.04890317]\n"
     ]
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "\n",
    "arr_b = np.random.rand(4)\n",
    "print(arr_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矢量积：\n",
      "[[1.20045777]\n",
      " [8.28055093]]\n",
      "\n",
      "数量积：\n",
      "9.481008708625925\n"
     ]
    }
   ],
   "source": [
    "#(3)\n",
    "import numpy as np\n",
    "\n",
    "arr_a_matrix = np.matrix(arr_a)\n",
    "arr_b_matrix = np.matrix(arr_b)\n",
    "\n",
    "# 计算矢量积\n",
    "vector_product = np.dot(arr_a_matrix, arr_b_matrix.T)\n",
    "print(\"矢量积：\")\n",
    "print(vector_product)\n",
    "\n",
    "# 计算数量积\n",
    "dot_product = np.sum(np.multiply(arr_a_matrix, arr_b_matrix))\n",
    "print(\"\\n数量积：\")\n",
    "print(dot_product)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  9 10]\n"
     ]
    }
   ],
   "source": [
    "#(4)\n",
    "import numpy as np\n",
    "\n",
    "arr_a = np.array([1, 2, 3, 4]) \n",
    "arr_a = np.append(arr_a, [9, 10]) \n",
    "print(arr_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2 11 12  3  4]\n"
     ]
    }
   ],
   "source": [
    "#(5)\n",
    "import numpy as np\n",
    "\n",
    "arr_a = np.array([1, 2, 3, 4])\n",
    "arr_a = np.insert(arr_a, 2, [11, 12])\n",
    "print(arr_a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3]\n"
     ]
    }
   ],
   "source": [
    "#(6)\n",
    "import numpy as np\n",
    "\n",
    "arr_a = np.array([1, 2, 3, 4])\n",
    "arr_a = np.delete(arr_a, np.arange(len(arr_a)) % 2 != 0)\n",
    "print(arr_a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "#(7)\n",
    "import numpy as np\n",
    "\n",
    "arr_a = np.array([1, 2, 3, 4])\n",
    "arr_a_list = arr_a.tolist()\n",
    "print(arr_a_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对角线上元素之和： 5\n",
      "重新变成3*3的二维数组：\n",
      " [[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "合并为3*6的数组：\n",
      " [[ 1  2  3 11 12 13]\n",
      " [ 4  5  6 14 15 16]\n",
      " [ 7  8  9 17 18 19]]\n",
      "水平切分后的数组：\n",
      "[[1 2]\n",
      " [4 5]\n",
      " [7 8]]\n",
      "[[ 3 11]\n",
      " [ 6 14]\n",
      " [ 9 17]]\n",
      "[[12 13]\n",
      " [15 16]\n",
      " [18 19]]\n",
      "\n",
      "垂直切分后的数组：\n",
      "[[ 1  2  3 11 12 13]]\n",
      "[[ 4  5  6 14 15 16]]\n",
      "[[ 7  8  9 17 18 19]]\n"
     ]
    }
   ],
   "source": [
    "#(8)\n",
    "import numpy as np\n",
    "\n",
    "# a.\n",
    "arr_a = np.array([[1, 2], [3, 4]])\n",
    "diagonal_sum = np.trace(arr_a)\n",
    "print(\"对角线上元素之和：\", diagonal_sum)\n",
    "\n",
    "# b.\n",
    "arr_b = np.arange(9)\n",
    "arr_b_2d = arr_b.reshape(3, 3)\n",
    "print(\"重新变成3*3的二维数组：\\n\", arr_b_2d)\n",
    "\n",
    "# c.\n",
    "arr_c1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "arr_c2 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])\n",
    "\n",
    "merged_3x6 = np.hstack((arr_c1, arr_c2))\n",
    "print(\"合并为3*6的数组：\\n\", merged_3x6)\n",
    "\n",
    "split_arrays = np.hsplit(merged_3x6, 3)  # 水平切分\n",
    "print(\"水平切分后的数组：\")\n",
    "for arr in split_arrays:\n",
    "    print(arr)\n",
    "\n",
    "split_6x3 = np.vsplit(merged_3x6, 3)  # 垂直切分\n",
    "print(\"\\n垂直切分后的数组：\")\n",
    "for arr in split_6x3:\n",
    "    print(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 2 5 0 1]]\n",
      "[[3 4 7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "#(1)\n",
    "import numpy as np\n",
    "\n",
    "a = [6, 2, 5, 0, 1, 3, 4, 7, 8, 9]\n",
    "b = np.array(a).reshape(2, 5)\n",
    "\n",
    "array1, array2 = np.vsplit(b, 2)\n",
    "\n",
    "print(array1)\n",
    "print(array2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 8  5  6  5  8]\n",
      " [ 5  7  8 13 16]]\n"
     ]
    }
   ],
   "source": [
    "#(2)\n",
    "import numpy as np\n",
    "\n",
    "a = [6, 2, 5, 0, 1, 3, 4, 7, 8, 9]\n",
    "b = ['2', '3', '1', 5, 7]\n",
    "\n",
    "# 将b序列中的字符串元素转换为整数\n",
    "b = [int(i) for i in b]\n",
    "\n",
    "array_a = np.array(a).reshape(2, 5)\n",
    "array_b = np.array(b).reshape(1, 5)\n",
    "\n",
    "result = array_a + array_b\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "序列a去重元素: [0 1 2 3 4 5 6 7 8 9]\n",
      "序列a去重元素的重复数量: [1 1 1 1 1 1 1 1 1 1]\n",
      "序列b去重元素: ['1' '2' '3' '5' '5.9' '7' 'a']\n",
      "序列b去重元素的重复数量: [1 1 1 1 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "#(3)\n",
    "a = [6, 2, 5, 0, 1, 3, 4, 7, 8, 9]\n",
    "b = ['2', 'a', '3', '1', 5, 7, 5.9]\n",
    "\n",
    "# 去重并统计元素重复数量\n",
    "unique_elements_a, counts_a = np.unique(a, return_counts=True)\n",
    "unique_elements_b, counts_b = np.unique(b, return_counts=True)\n",
    "\n",
    "print(\"序列a去重元素:\", unique_elements_a)\n",
    "print(\"序列a去重元素的重复数量:\", counts_a)\n",
    "\n",
    "print(\"序列b去重元素:\", unique_elements_b)\n",
    "print(\"序列b去重元素的重复数量:\", counts_b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "序列c: [1.1, 2.1, 3.3, 4.3, 7.7, 8.7, 9.9, 10.9]\n"
     ]
    }
   ],
   "source": [
    "#(4)\n",
    "a = [1.1, 3.3, 7.7, 9.9]\n",
    "b = [0.0, 2.2, 4.4, 6.6, 8.8]\n",
    "\n",
    "c = []\n",
    "for num in a:\n",
    "    c.append(num)\n",
    "    if num + 1 not in b:  # 寻找与a序列中元素连续的b序列元素\n",
    "        c.append(num + 1)\n",
    "\n",
    "print(\"序列c:\", c)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、总结以下知识点内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、数组广播机制，并举例说明；\n",
    "#### （1）如果两个数组的维度不同，那么形状较小的数组会在其前面加上一个长度为1的维度，直到两个数组的维度数量相同。\n",
    "#### （2）当两个数组在某个维度的大小不同时，NumPy会沿着维度为1的数组进行扩展，使得这个维度的大小与另一个数组相同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6 7]\n",
      " [6 7 8]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "#例：\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3])\n",
    "b = np.array([[4], [5], [6]])\n",
    "\n",
    "result = a + b\n",
    "\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据广播机制的规则，数组a会被视为形状为(1, 3)的二维数组，而数组b为形状为(3, 1)的二维数组。在进行加法运算时，NumPy会将这两个数组的形状扩展为(3, 3)，然后对应元素相加，最终得到结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、列举numpy中随机数生成函数，添加删除数组元素的方法，数组合并与拆分，并举例说明；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "#添加元素：\n",
    "arr = np.array([1, 2, 3])\n",
    "arr = np.append(arr, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除元素：\n",
    "arr = np.delete(arr, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "#合并数组：\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "merged_arr = np.concatenate((arr1, arr2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "#拆分数组：\n",
    "arr = np.array([[1, 2], [3, 4]])\n",
    "split_arr = np.split(arr, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机生成的浮点数数组：\n",
      "[[0.43455491 0.95874651 0.57980848]\n",
      " [0.37862931 0.35732482 0.10668941]\n",
      " [0.45351901 0.11674616 0.84503126]]\n",
      "添加元素后的数组：\n",
      "[1 2 3 4]\n",
      "删除第一个元素后的数组：\n",
      "[2 3 4]\n",
      "合并数组后的结果：\n",
      "[1 2 3 4 5 6]\n",
      "拆分数组后的结果：\n",
      "[array([[1, 2]]), array([[3, 4]])]\n"
     ]
    }
   ],
   "source": [
    "#例：\n",
    "import numpy as np\n",
    "\n",
    "# 随机数生成示例\n",
    "rand_nums = np.random.rand(3, 3)\n",
    "print(\"随机生成的浮点数数组：\")\n",
    "print(rand_nums)\n",
    "\n",
    "# 添加元素示例\n",
    "arr = np.array([1, 2, 3])\n",
    "arr = np.append(arr, 4)\n",
    "print(\"添加元素后的数组：\")\n",
    "print(arr)\n",
    "\n",
    "# 删除元素示例\n",
    "arr = np.delete(arr, 0)\n",
    "print(\"删除第一个元素后的数组：\")\n",
    "print(arr)\n",
    "\n",
    "# 数组合并与拆分示例\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "merged_arr = np.concatenate((arr1, arr2))\n",
    "print(\"合并数组后的结果：\")\n",
    "print(merged_arr)\n",
    "\n",
    "arr = np.array([[1, 2], [3, 4]])\n",
    "split_arr = np.split(arr, 2)\n",
    "print(\"拆分数组后的结果：\")\n",
    "print(split_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、Numpy代数运算，并举例说明；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵转置结果：\n",
      "[[1 3]\n",
      " [2 4]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵转置：np.transpose()\n",
    "# 创建一个矩阵\n",
    "A = np.array([[1, 2], [3, 4]])\n",
    "\n",
    "# 计算矩阵转置\n",
    "A_transpose = np.transpose(A)\n",
    "print(\"矩阵转置结果：\")\n",
    "print(A_transpose)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、列举NumPy矩阵创建方法、及操作方法（矩阵常用的操作有矩阵转置、索引取值、求矩阵的行列数、排序和将列表转换成矩阵等），并举例说明；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵创建方法：\n",
    "\n",
    "使用np.array()函数从Python列表创建矩阵。\n",
    "使用np.zeros()函数创建全为0的矩阵。\n",
    "使用np.ones()函数创建全为1的矩阵。\n",
    "使用np.random.rand()函数创建随机数填充的矩阵。\n",
    "\n",
    "矩阵操作方法：\n",
    "\n",
    "矩阵转置：使用np.transpose()、或者.T属性进行矩阵转置。\n",
    "索引取值：使用索引和切片操作获取矩阵中的元素。\n",
    "求矩阵的行列数：使用.shape属性获取矩阵的行数和列数。\n",
    "排序：使用np.sort()对矩阵进行排序。\n",
    "将列表转换成矩阵：可以直接使用np.array()将列表转换成矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵转置结果：\n",
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n",
      "矩阵索引取值结果： 6\n",
      "矩阵的行数和列数： 3 3\n",
      "矩阵排序结果：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "转换后的矩阵：\n",
      "[[10 11 12]\n",
      " [13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "#例：\n",
    "import numpy as np\n",
    "\n",
    "# 创建矩阵\n",
    "matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "\n",
    "# 矩阵转置\n",
    "transposed_matrix = np.transpose(matrix)\n",
    "print(\"矩阵转置结果：\")\n",
    "print(transposed_matrix)\n",
    "\n",
    "# 索引取值\n",
    "element = matrix[1, 2]\n",
    "print(\"矩阵索引取值结果：\", element)\n",
    "\n",
    "# 求矩阵的行列数\n",
    "rows, cols = matrix.shape\n",
    "print(\"矩阵的行数和列数：\", rows, cols)\n",
    "\n",
    "# 对矩阵进行排序\n",
    "sorted_matrix = np.sort(matrix, axis=None)\n",
    "print(\"矩阵排序结果：\")\n",
    "print(sorted_matrix)\n",
    "\n",
    "# 将列表转换成矩阵\n",
    "my_list = [[10, 11, 12], [13, 14, 15]]\n",
    "converted_matrix = np.array(my_list)\n",
    "print(\"转换后的矩阵：\")\n",
    "print(converted_matrix)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
